#include "common.h"

#ifndef __TS_DIQUE__
#define __TS_DIQUE__

#include "mutex.h"

namespace GXL_Engine
{
 template<typename T, typename LOCK = Mutex>
	class TSDique 
	{
	public:
		VOID	PushBack(const T& obj);
		VOID	PushFront(const T& obj);
		T		PopBack();
		T		PopFront();
		INT32	Size();
	private:
		LOCK			m_Lock;
		std::list<T>	m_List;
	};

	template<typename T, typename LOCK>
	VOID TSDique<T, LOCK>::PushBack( const T& obj )
	{
		m_Lock.Acquire();
		m_List.push_back(obj);
		m_Lock.Release();
	}

	template<typename T, typename LOCK>
	T TSDique<T, LOCK>::PopBack()
	{
		m_Lock.Acquire();
		if (m_List.size() <= 0)
		{
			m_Lock.Release();
			return T(-1);
		}
		T obj = m_List.back();
		m_List.pop_back();
		m_Lock.Release();
		return obj;
	}

	template<typename T, typename LOCK>
	VOID TSDique<T, LOCK>::PushFront( const T& obj )
	{
		m_Lock.Acquire();
		m_List.push_front(obj);
		m_Lock.Release();
	}

	template<typename T, typename LOCK>
	T TSDique<T, LOCK>::PopFront()
	{
		m_Lock.Acquire();
		if (m_List.size() <= 0)
		{
			m_Lock.Release();
			return T(-1);
		}
		T obj = m_List.front();
		m_List.pop_front();
		m_Lock.Release();
		return obj;
	}
	template<typename T, typename LOCK>
	INT32 Size()
	{
		m_Lock.Acquire();
		INT32 nSize = m_List.SIZE();
		m_Lock.Release();
		return nSize;
	}
}//namespace GXL_Engine
#endif //__TS_DIQUE__